// 获取库存列表（带分页和筛选）
const db = uniCloud.database();
const dbCmd = db.command;
const inStockCollection = db.collection("inStock");
const outStockCollection = db.collection("outStock");

// 获取入库记录列表
exports.getStockStatistic = async (page = 1, pageSize = 10, filters = {}) => {
  try {
    // 处理过滤条件
    let enhancedFilter = {
      //未删除
      isDel: 0,
    }; // 基础过滤条件

    // 移除 filters 中的空值、undefined 和 null
    Object.keys(filters).forEach((key) => {
      if (
        filters[key] !== undefined &&
        filters[key] !== null &&
        filters[key] !== ""
      ) {
        enhancedFilter[key] = filters[key];
      }
    });

    // 获取入库记录并进行分页
    const inStockResult = await inStockCollection.where(enhancedFilter).get();
    // 获取销售记录
    const outStockResult = await outStockCollection.where(enhancedFilter).get();

    console.log(inStockResult, "====inStockResult====");
    console.log(outStockResult, "====outStockResult====");
    // 返回结果，包含分页数据
    return {
      code: 0,
      message: "查询成功",
      data: result.data,
      total: countResult.total,
    };
  } catch (error) {
    console.log(error);
    return {
      code: 1,
      message: "查询失败",
      error,
    };
  }
};
